package net.hn.hnms.biz.shipped.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.hn.hnms.biz.shipped.domain.TransportDeliveryInfo;
import net.hn.hnms.biz.shipped.mapper.TransportDeliveryInfoMapper;
import net.hn.hnms.biz.shipped.request.TransportDeliveryInfoReq;
import net.hn.hnms.biz.shipped.service.ITransportDeliveryInfoService;
import net.hn.hnms.biz.shipped.vo.TransportDeliveryInfoVO;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wangjr
 * 发运流水表服务层实现
 * @date 2022-08-30
 */

@Service
@DS("master")
public class TransportDeliveryInfoServiceImpl implements ITransportDeliveryInfoService {

    @Autowired
    private TransportDeliveryInfoMapper transportDeliveryInfoMapper;

    @Override
    public ResponsePage<TransportDeliveryInfoVO> selectPageList(TransportDeliveryInfoReq req, PageQuery pageQuery) {
        IPage<TransportDeliveryInfoVO> result = transportDeliveryInfoMapper.selectVoPage(pageQuery.build(), buildQuery(req));
        return new ResponsePage(result.getRecords(), (int) result.getTotal());
    }

    @Override
    public List<Map<String, Object>> dictList( String mineCode) {
        List<Map<String, Object>> result = new ArrayList<>();

        LambdaQueryWrapper<TransportDeliveryInfo> queryCoalName = new LambdaQueryWrapper<>();
        queryCoalName.eq(StrUtil.isNotBlank(mineCode),TransportDeliveryInfo::getMineCode,mineCode);
        queryCoalName.groupBy(TransportDeliveryInfo::getCoalName);
        queryCoalName.select(TransportDeliveryInfo::getCoalName);
        List<Map<String, Object>> coalNames = transportDeliveryInfoMapper.selectMaps(queryCoalName);
        if(CollUtil.isNotEmpty(coalNames)) result.addAll(coalNames);

        LambdaQueryWrapper<TransportDeliveryInfo> queryDept = new LambdaQueryWrapper<>();
        queryDept.eq(StrUtil.isNotBlank(mineCode),TransportDeliveryInfo::getMineCode,mineCode);
        queryDept.groupBy(TransportDeliveryInfo::getDeptName);
        queryDept.select(TransportDeliveryInfo::getDeptName);
        List<Map<String, Object>> deptNames = transportDeliveryInfoMapper.selectMaps(queryDept);
        if(CollUtil.isNotEmpty(deptNames)) result.addAll(deptNames);
        return result;
    }

    private LambdaQueryWrapper<TransportDeliveryInfo> buildQuery(TransportDeliveryInfoReq req){
        LambdaQueryWrapper<TransportDeliveryInfo> query = Wrappers.lambdaQuery();
        query.like(StrUtil.isNotBlank(req.getCustName()),TransportDeliveryInfo::getCustName,req.getCustName());
        query.like(StrUtil.isNotBlank(req.getVehicleNo()),TransportDeliveryInfo::getVehicleNo,req.getVehicleNo());
        query.like(StrUtil.isNotBlank(req.getCarrierCompanyName()),TransportDeliveryInfo::getCarrierCompanyName,req.getCarrierCompanyName());
        query.like(StrUtil.isNotBlank(req.getBusinessEgistrationName()),TransportDeliveryInfo::getBusinessEgistrationName,req.getBusinessEgistrationName());

        query.eq(StrUtil.isNotBlank(req.getCoalName()),TransportDeliveryInfo::getCoalName,req.getCoalName());
        query.eq(StrUtil.isNotBlank(req.getBusinessType()),TransportDeliveryInfo::getType,req.getBusinessType());
        query.eq(StrUtil.isNotBlank(req.getDeptName()),TransportDeliveryInfo::getDeptName,req.getDeptName());
        query.eq(StrUtil.isNotBlank(req.getSettleState()),TransportDeliveryInfo::getSettleState,req.getSettleState());
        query.eq(StrUtil.isNotBlank(req.getVoucherNo()),TransportDeliveryInfo::getVoucherNo,req.getVoucherNo());
        query.eq(StrUtil.isNotBlank(req.getVehicleNo()),TransportDeliveryInfo::getVehicleNo,req.getVehicleNo());
        query.eq(StrUtil.isNotBlank(req.getIsRegName()),TransportDeliveryInfo::getIsRegName,req.getIsRegName());
        query.eq(StrUtil.isNotBlank(req.getState()),TransportDeliveryInfo::getState,req.getState());
        query.eq(StrUtil.isNotBlank(req.getMineCode()),TransportDeliveryInfo::getMineCode,req.getMineCode()).orderByDesc(TransportDeliveryInfo::getSendTareTime);

        if(req.getBeginDate() != null) query.apply(" create_time >= to_timestamp('"+ DateUtil.formatDate(req.getBeginDate())+"','yyyy-mm-dd hh24:mi:ss') ");
        if(req.getEndDate() != null) query.apply(" create_time <= to_timestamp('"+DateUtil.formatDate(req.getEndDate())+"','yyyy-mm-dd hh24:mi:ss') ");
        return query;
    }
}
